from django.contrib import admin
from .models import Comment, Like, Favorite, Notification, Follow, Report

class CommentAdmin(admin.ModelAdmin):
    """评论管理界面"""
    list_display = ('user', 'story', 'content_preview', 'status', 'is_reply', 'created_at')
    list_filter = ('status', 'created_at')
    search_fields = ('user__username', 'story__title', 'content')
    readonly_fields = ('user', 'story', 'parent_comment', 'created_at')
    date_hierarchy = 'created_at'
    actions = ['approve_comments', 'reject_comments']
    
    def content_preview(self, obj):
        """显示评论内容预览"""
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = '评论内容'
    
    def approve_comments(self, request, queryset):
        """批量通过评论"""
        queryset.update(status='approved', rejected_reason=None)
    approve_comments.short_description = "通过选中的评论"
    
    def reject_comments(self, request, queryset):
        """批量拒绝评论"""
        queryset.update(status='rejected', rejected_reason='内容不符合社区规范')
    reject_comments.short_description = "拒绝选中的评论"
    
    def get_queryset(self, request):
        """优先显示待审核的评论"""
        qs = super().get_queryset(request)
        return qs.order_by('status', '-created_at')

class NotificationAdmin(admin.ModelAdmin):
    list_display = ['recipient', 'sender', 'notification_type', 'content_preview', 'is_read', 'created_at']
    list_filter = ['notification_type', 'is_read', 'created_at']
    search_fields = ['recipient__username', 'sender__username', 'content']
    
    def content_preview(self, obj):
        """显示通知内容预览"""
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = '通知内容'

class ReportAdmin(admin.ModelAdmin):
    list_display = ['reporter', 'story', 'report_type', 'is_handled', 'created_at']
    list_filter = ['report_type', 'is_handled', 'created_at']
    search_fields = ['reporter__username', 'story__title', 'description']
    date_hierarchy = 'created_at'
    actions = ['mark_as_handled']
    
    def mark_as_handled(self, request, queryset):
        """将举报标记为已处理"""
        queryset.update(is_handled=True)
    mark_as_handled.short_description = "标记为已处理"

admin.site.register(Comment, CommentAdmin)
admin.site.register(Notification, NotificationAdmin)
admin.site.register(Report, ReportAdmin)
admin.site.register(Like)
admin.site.register(Favorite)
admin.site.register(Follow)
